package com.huawang.mdesign.model.service.constant;

import com.huawang.core.definition.mof.UMLDefinitionConstants;
import com.huawang.core.definition.mof.cmof.reflection.RElement;
import com.huawang.core.definition.mof.instances.MetaClassInstance;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * @author cdl
 */
@Getter
@AllArgsConstructor
public enum TransitionEffectEnum {

    ACTIVITY(UMLDefinitionConstants.Activity, "1"),
    STATE_MACHINE(UMLDefinitionConstants.StateMachine, "2"),
    INTERACTION(UMLDefinitionConstants.Interaction, "3"),
    OPAQUE_BEHAVIOR(UMLDefinitionConstants.OpaqueBehavior, "4"),
    ;

    private final String metaClass;

    private final String type;

    @NotNull
    public static String getType(MetaClassInstance instance) {
        String metaClass = Optional.ofNullable(instance)
                .map(MetaClassInstance::getMetaClass)
                .map(RElement::getId)
                .orElse("");

        return Arrays.stream(TransitionEffectEnum.values())
                .filter(e -> Objects.equals(e.metaClass, metaClass))
                .map(TransitionEffectEnum::getType)
                .findFirst()
                .orElse("");
    }

    public static boolean isStateMachine(String type) {
        return Objects.equals(type, STATE_MACHINE.type);
    }

    public static boolean isOpaqueBehavior(String type) {
        return Objects.equals(type, OPAQUE_BEHAVIOR.type);
    }
}
